using UnityEngine;

namespace FutureCorePlugin
{
    public class BasePreUI<T> : MonoBehaviour where T : BasePreUI<T>
    {
        private static T m_instance;
        public static T Instance
        {
            get
            {
                if (m_instance == null)
                {
                    if (IsAppQuit)
                    {
                        return null;
                    }
                    m_instance = CreateInstance();
                }
                return m_instance;
            }
        }

        private static bool IsAppQuit;

        private static T CreateInstance()
        {
            return PreUIHandler.Instance.CreateInstanceUI<T>();
        }

        protected virtual void OnDestroy()
        {
            m_instance = null;
        }

        private void OnApplicationQuit()
        {
            IsAppQuit = true;
            if (m_instance == null) return;
            Destroy(m_instance.gameObject);
        }

        [HideInInspector]
        public string resPath;
        [HideInInspector]
        public PreUIHandler.UILayer uiLayer;
        [HideInInspector]
        public int uiSortingOrder;
        [HideInInspector]
        public GameObject uiGO;
        [HideInInspector]
        public Transform uiTF;

        public void Init()
        {
            OnInit();
        }

        public void Bind()
        {
            OnBind();
        }

        public void Open(object param = null)
        {
            gameObject.SetActive(true);
            OnOpen(param);
        }

        public void Close()
        {
            if (gameObject != null)
            {
                Destroy(gameObject);
            }
        }

        public void Hide()
        {
            gameObject.SetActive(true);
        }

        protected virtual void OnInit() { }
        protected virtual void OnBind() { }
        protected virtual void OnOpen(object param) { }
    }
}